local ClientOHOH = _G.ClientOHOH;
local TweenService = game:GetService("TweenService")
local M = ClientOHOH.class.new(script,ClientOHOH.files.OHOHOLYBUIBase());
local CallBackManager = ClientOHOH.load.CallBackManager();
local Task_Cfg = _G.CommonOHOH.config.Task
local languageHelper =  _G.LanguageHelper

function InitInsts(ui)
    ui.insts = {};
    ui.insts.cell = "ctrl.cell"
    ui.insts.close = "ctrl.close"
    ui.insts.list = "ctrl.list"
end

local cellStr = {
    cell = "ctrl.cell",
    recieveBtn = "ctrl.btn.recieve",
    processBtn = "ctrl.btn.notRecieve",
    clipping = "ctrl.Clipping",
    name = "ctrl.name",
    icon = "ctrl.icon",
    taskDesc = "ctrl.taskDesc",
    processVal = "ctrl.processVal",
    AwardVal = "ctrl.rewardVal",
    AwardDesc = "ctrl.rewardDesc",
}

function M.new(...)
    return M(...);
end

function M.ctor(self)
    InitInsts(self)
    M.super.ctor(self,ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.TaskGui"))
    self.insts.cell.Visible = false
    self.missionList = {}
    self:AddConnect()
end

function M.AddConnect(self)
    CallBackManager:AddRbxConnect(self,self.insts.close.Activated,function()
		self:Hide()
	end)
end

function M.Init(self,taskName,progress)
        --获取任务在cfg的id
        local list = string.split(taskName,"_")
        local id =  tonumber(list[3])
        --创建任务ui，初始化
        local taskObj = self.insts.cell:Clone()
        local mission = self:InitUiInst2(taskObj,cellStr)
        self:InitUiObj(taskObj)
        taskObj.Parent = self.insts.list
        taskObj.Name = taskName
        --填充ui信息
        self:SetBaseInfo(mission,id,progress)
        mission.cell.Visible = true
        table.insert(self.missionList,mission)
        return #self.missionList
end

--设置基础信息
function M.SetBaseInfo(self,mission,id,progress)
    mission.name.Text = languageHelper:GetText(Task_Cfg[id].name)
    mission.icon.Image = Task_Cfg[id].icon
    mission.taskDesc.Text = languageHelper:GetText(Task_Cfg[id].taskDesc)
    mission.AwardDesc.Text = languageHelper:GetText(Task_Cfg[id].AwardDesc)
    mission.AwardVal.Text = languageHelper:GetText(Task_Cfg[id].AwardValueDesc)
    local pVal = math.clamp(progress/Task_Cfg[id].goalValue,0,1)
    if pVal==1 then
        mission.recieveBtn.Visible = true
        mission.processBtn.Visible = false
    end
    mission.clipping.Size = UDim2.new(pVal,0,1,0)
    mission.processVal.Text = progress.."/"..Task_Cfg[id].goalValue
end

--更新进度
function M.UpdateProgress(self,now,finish,index)
    self.missionList[index].processVal.Text = now.."/"..finish
    local pVal = math.clamp(now/finish,0,1)
    self.missionList[index].clipping.Size = UDim2.new(pVal,0,1,0)
end

--是否显示领取，true显示，false显示进行中
function M.IsShowClaim(self,index,isShow)
    self.missionList[index].processBtn.Visible = not isShow
    self.missionList[index].recieveBtn.Visible = isShow
end

--将任务更新为下一个任务,index为mission在missionlist中的index，id为taskId：101
function M.UpdateNextTask(self,index,id)
    local mission = self.missionList[index]
    self:SetBaseInfo(mission,id,0)
end


function M.dtor(self)
    M.super.dtor(self);
end

return M